﻿Imports System.Data.OleDb
Public Class editprofile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then

            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            Dim SqlString As String = "SELECT * FROM Profile WHERE DieterName=@f1"

            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("FirstName")) Then firstName.Text = dr("FirstName")
                If Not IsDBNull(dr("LastName")) Then lastName.Text = dr("LastName")

                If Not IsDBNull(dr("Age")) Then Age.Text = dr("Age")
                If Not IsDBNull(dr("Description")) Then desc_TB.Text = dr("Description")
                If Not IsDBNull(dr("StartingWeight")) Then startWeight.Text = dr("StartingWeight")
                If Not IsDBNull(dr("TargetWeight")) Then targetWeight.Text = dr("TargetWeight")
            End If
        End If

    End Sub

    Protected Function GetImageSource() As String
        '** this section locates the image file path from the database so that the image can be show outside of gridview**'

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim SqlString As String = "SELECT PROFILEPICTURE from Profile WHERE DieterName=@f1"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        Dim imageSource As String
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        oleDbConn.Open()
        Dim imagepath = cmd.ExecuteScalar()
        imageSource = "/" + imagepath + ""

        Return imageSource

    End Function

    Protected Sub BTN_profile_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BTN_profile.Click

        Dim newFileName As String = ""
        Dim SqlString As String
        Dim fileOK As Boolean = False
        If FileUpload_Picture.HasFile Then
            Dim myGUID As New System.Guid
            myGUID = Guid.NewGuid()
            newFileName = myGUID.ToString() + ".jpg"


            If FileUpload_Picture.HasFile Then
                Dim fileExtension As String
                fileExtension = System.IO.Path.GetExtension(FileUpload_Picture.FileName).ToLower()
                Dim allowedExtensions As String() = {".jpg", ".jpeg"}
                For i As Integer = 0 To allowedExtensions.Length - 1
                    If fileExtension = allowedExtensions(i) Then
                        fileOK = True
                    End If

                Next
                If fileOK Then
                    FileUpload_Picture.SaveAs(Request.MapPath("pictures") & "/" & newFileName)

                End If

            End If

        End If



        If FileUpload_Picture.HasFile And fileOK Then
            SqlString = "UPDATE Profile SET FirstName=@F1, LastName=@F2, Age=@F3, Description=@F4, StartingWeight=@F5, TargetWeight=@F6, ProfilePicture=@F7 WHERE DieterName=@f8"
        Else
            SqlString = "UPDATE Profile SET FirstName=@F1, LastName=@F2, Age=@F3, Description=@F4, StartingWeight=@F5, TargetWeight=@F6 WHERE DieterName=@f8"
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", firstName.Text)
        cmd.Parameters.AddWithValue("@f2", lastName.Text)
        cmd.Parameters.AddWithValue("@f3", Age.Text)
        cmd.Parameters.AddWithValue("@f4", desc_TB.Text)
        cmd.Parameters.AddWithValue("@f5", startWeight.Text)
        cmd.Parameters.AddWithValue("@f6", targetWeight.Text)
        If FileUpload_Picture.HasFile Then
            cmd.Parameters.AddWithValue("@f7", "Pictures/" & newFileName)
        End If

        cmd.Parameters.AddWithValue("@f8", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()

        Dim dateString As String
        dateString = Date.Now.ToString("dd/MM/yyyy")
        Dim weightSql As String = "INSERT INTO weights(Member, Weight, Posted) Values (@f1,@f2,@f3)"
        Dim weightcmd As OleDbCommand = New OleDbCommand(weightSql, oleDbConn)
        weightcmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        weightcmd.Parameters.AddWithValue("@f2", startWeight.Text)
        weightcmd.Parameters.AddWithValue("@f3", dateString)

        weightcmd.ExecuteNonQuery()
        oleDbConn.Close()


        oleDbConn.Dispose()
        Response.Redirect("profile.aspx?DieterName=" & User.Identity.Name)

    End Sub
End Class